package com.china08.yxyapi.api.manage.sch.platform.website.template;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.china08.yxyapi.api.YxyApiBaseController;
import com.china08.yxyapi.entity.db.sws.SwsSchoolTemplet;
import com.china08.yxyapi.entity.db.sws.SwsSetting;
import com.china08.yxyapi.entity.db.sws.SwsTempletModule;
import com.china08.yxyapi.model.YxyApiReqModel;
import com.china08.yxyapi.model.YxyApiRespModel;
import com.china08.yxyapi.repository.db.SwsSchoolTempletRepository;
import com.china08.yxyapi.repository.db.SwsSettingRepository;
import com.china08.yxyapi.repository.db.SwsTempletModuleRepository;

@CrossOrigin
@RestController
public class SwsTemplateUpdV1Controller extends YxyApiBaseController {

    @Autowired
    private SwsSettingRepository swsSettingRepository;

    @Autowired
    private SwsSchoolTempletRepository swsSchoolTempletRepository;

    @Autowired
    SwsTempletModuleRepository SwsTempletModuleRepository;

    @RequestMapping(value = "/api_yw/manage/sch/platform/website/template/upt", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public Object precess(HttpServletRequest request, HttpServletResponse response, @RequestBody(required = false) ReqModel reqModel) {
        long l1 = System.currentTimeMillis();
        String schoolId = getProfile(request, response).getSchoolId();
        String templateId = reqModel.templateId;
        String skinTheme = reqModel.skinTheme;
        String skinColor = reqModel.skinColor;

        Assert.isTrue(StringUtils.isNotBlank(templateId), "模板ID为空");
        Assert.isTrue(StringUtils.isNotBlank(skinTheme), "主题为空");

        // 修改配色和主题
        SwsSetting swsSetting = swsSettingRepository.findBySchoolId(schoolId);
        swsSetting.setSkinTempl(templateId);
        swsSetting.setSkinTheme(skinTheme);

        // 配色不为空
        if (StringUtils.isNotBlank(skinColor)) {
            swsSetting.setSkinColor(skinColor);
        }
        swsSettingRepository.save(swsSetting);

        // 删除模块信息，重新添加
        List<SwsSchoolTemplet> templetModules = swsSchoolTempletRepository.findBySchoolIdAndTemplateId(schoolId, templateId);
        swsSchoolTempletRepository.delete(templetModules);

        // 所有模块
        List<SwsTempletModule> swsTempletModules = SwsTempletModuleRepository.findByTemplateId(templateId);

        for (SwsTempletModule swsTempletModule : swsTempletModules) {
            SwsSchoolTemplet templetModule = new SwsSchoolTemplet();
            templetModule.setTemplateId(templateId);
            templetModule.setSchoolId(schoolId);
            templetModule.setModuleId(swsTempletModule.getModuleId());
            swsSchoolTempletRepository.save(templetModule);
        }
        RespModel respModel = new RespModel();

        respModel.setMessage("修改成功");
        System.out.println(System.currentTimeMillis() - l1);
        return respModel;
    }

    protected static class ReqModel extends YxyApiReqModel {

        public String templateId;

        public String skinTheme;

        public String skinColor;
    }

    protected class RespModel extends YxyApiRespModel {
        public RespModel() {

        }

    }
}
